﻿#include "qt_hread_process.h"




void callback(Mat image, void* p)
{
	QtThreadProcess* pCammerWidget = static_cast<QtThreadProcess*>(p);
        pCammerWidget->m_qDisplayFrameQueue.push_back(image);
        pCammerWidget->m_image_index++;
}

QtThreadProcess::QtThreadProcess(QObject* parent) 
{
    m_proess_state = false;
    m_only_show_state = false;

    qRegisterMetaType<Mat>("Mat");
    //对于自定义的结构体参数，信号槽无法识别参数，导致信号槽连接不起作用。
    //所以需要注册结构体参数。在结构体中声明结束的地方加上结构体注册。
    qRegisterMetaType<ShowResult>("ShowResult");



	m_timer_frame_rate = new QTimer(this);
	connect(m_timer_frame_rate, &QTimer::timeout, this, [=]() {
		m_frame_rate = m_image_index - m_image_index_last;
		m_image_index_last = m_image_index;
	});
	m_timer_frame_rate->start(1000);//固定频率读取离线测试图像
	
}

void QtThreadProcess::run()
{
    while (1)
    {
        Mat image;
        if (false == m_qDisplayFrameQueue.get(image))
        {
            continue;
        }

        if (m_proess_state == true)
        {
            int64 t1 = cv::getTickCount();
            ShowResult result;

            HObject hobject = matToHImage(image);
            result.m_detectin_result = m_function->detection(hobject.Clone(), m_detection_index%m_times,result.m_messgae);
            result.m_camera_index = m_camera_index;
            result.m_detection_index = m_detection_index%m_times;
            result.m_camera_name = m_camera_name;
            result.m_save_right = m_function->m_save_right;
            result.m_save_wrong = m_function->m_save_wrong;
            result.m_save_ai_ok = m_function->m_save_ai_ok;
            result.m_save_wrong_label = m_function->m_save_wrong_label;
            result.m_save_ai_ng          = m_function->m_save_ai_ng;
			result.m_times            = m_times;

            m_vector_result.push_back(result.m_detectin_result);

			if (m_times != 3)
			{
				if (m_vector_result.size() == m_times)
				{
					int number = 0;
					for (int i = 0; i < m_vector_result.size(); i++)
					{
						if (m_vector_result[i])

							number = number + 1;
						else
							number = number + 0;
					}
					if (number == m_times)
					{
						m_ok0_count = m_ok0_count + 1;
						result.m_output_result.push_back(true);
					}
					else
					{
						m_ng0_count = m_ng0_count + 1;
						result.m_output_result.push_back(false);
					}
					m_vector_result.clear();
				}
			}
			else
			{
				if (m_vector_result.size() == m_times)
				{
					int number = 0;
					for (int i = 0; i < m_vector_result.size(); i++)
					{
						if (m_vector_result[i])
							number = number + 1;
						else
							number = number + 0;
					}
					if (m_vector_result[0])
						m_ok0_count = m_ok0_count + 1;
					else
						m_ng0_count = m_ng0_count + 1;
					if (m_vector_result[1])
						m_ok1_count = m_ok1_count + 1;
					else
						m_ng1_count = m_ng1_count + 1;
					if (m_vector_result[2])
						m_ok2_count = m_ok2_count + 1;
					else
						m_ng2_count = m_ng2_count + 1;
					if (number == m_times)
					{		
						result.m_output_result.push_back(true);
					}
					else
					{
						result.m_output_result.push_back(false);
					}
					m_vector_result.clear();
				}
			}

			

            int64 t2 = cv::getTickCount();

            m_time_use = (t2 - t1) * 1000 / getTickFrequency();
            result.m_time_use = m_time_use;


            result.m_image = image;
            result.m_frame_rate = m_frame_rate;
            

            result.m_image_ai = m_function->m_image_ai;
			m_function->draw(image, m_detection_index%m_times, result.m_image_draw);

            emit sendDetectionResult(result);


            m_detection_index++;
        }
        if (m_only_show_state == true)
        {
                emit sendShowImage(image, m_camera_index, m_image_index);
        }
    }
}



